class Solution
{
public:
    int eraseOverlapIntervals(vector<vector<int>> &intervals)
    {
        auto compare = [](auto &l, auto &r)
        {
            return l[1] < r[1];
        };
        sort(intervals.begin(), intervals.end(), compare);
        int selected = 0;
        int end = intervals[0][0];
        for (auto &interval : intervals)
        {
            if (interval[0] >= end)
            {
                ++selected;
                end = interval[1];
            }
        }
        return intervals.size() - selected;
    }
};